/*Wrote by Ken on 29/2/2012
Test a method called "updateSEPoints(Player, int)" in the "EventController" class
The situation is when the controller want to add 1 points to the player
2012-04-12 Syrings Tam, Add testUpdateSEpoint2
*/
package testEvent;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import junit.framework.TestCase;
import org.junit.*;

import card.EventCard;
import dice.RollDice;
import event.*;
import player.*;

public class testEventController extends TestCase {
	@After
	public void tearDown() {}
	
	@Test
	public void testUpdateSEpoint1(){
		Player p = new Player();
		int beforeAns = p.getSEPoint();
		EventController eC = new EventController();
		
		eC.updateSEPoints(p, 1); // Add 1 SE point to user, the update is a void method
		int afterAnswer = p.getSEPoint();
		int expectedResult = beforeAns+1;
		assertEquals(expectedResult, afterAnswer );
	}
	
	@Test
	public void testUpdateSEpoint2(){
		Player p = new Player();
		int beforeAns = p.getSEPoint();
		EventController eC = new EventController();
		
		eC.updateSEPoints(p, -1); // Add 1 SE point to user, the update is a void method
		int actual = p.getSEPoint();
		int expected = beforeAns-1;
		assertEquals(expected, actual);
	}
	
	@Test
	public void testMarker(){
		EventCard card = new EventCard();
		EventController eC = new EventController();		
		class RollDiceStub extends RollDice{
			@Override
			public int roll(int number){
				return 1;
			}
		}
		RollDiceStub rd = new RollDiceStub();
		int diceNo = rd.roll(1)-1;
		int score = eC.marker(1, card.getEventArr()[diceNo]);
		assertEquals(3,score);
	}
	
	@Test
	public void testGetSelectedEvent(){
		EventController eC = new EventController();	
		
		class RollDiceStub extends RollDice{
			@Override
			public int roll2(int number){
				return 1;
			}
		}
		RollDiceStub rd = new RollDiceStub();
		int diceNo = rd.roll2(1);

		int actual = eC.getSelectedEvent(diceNo).getEventID();
		assertEquals(0,actual);
	}
	
	@Test
	public void testRandomEvent() throws Exception {
		String data = "2\n\n";
		String result;
		String heading = "You have picked Event 1";
		String event = "Background: Children always go out with make-up, seem like having a relationship.\nHowever, mother is a traditional person.\nShe does not like her children falling in love with others when they are young.\nSo, daddy, how can you solve this problem?\n";
		
		InputStream testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
		InputStream old = System.in;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		PrintStream testOutPrintStream = new PrintStream(testOut);
		System.setOut(testOutPrintStream);
		
		try {
		    System.setIn( testInput );
		    
		    EventController eC = new EventController();	
		    Player p = new Player();
		    eC.RandomEvent(p);

		} finally {
		    System.setIn( old );
		}

		String newLine = System.getProperty("line.separator");
		if (newLine == null) newLine = "\n";
		result=heading + newLine + event + newLine;			
		
		assertEquals(result, testOut.toString());

	}
}